<?php 
class AdTypeAdmin
{
	public function runPreActions()
	{
		global $commander;
		global $adc;
		switch($commander->getPostAction())
		{	
			case 'list_type':
				if($_POST['new_item_title'] != '')
				{ self::addAdType($_POST['new_item_title']); }
				if($_POST['activate'] > 0)
				{ $commander->setTableActive($_POST['activate'], 'am_types', 1); }
				if($_POST['deactivate'] > 0)
				{ $commander->setTableActive($_POST['deactivate'], 'am_types', 0); }
				if($_POST['del_item_id'] > 0)
				{ self::deleteAdType($_POST['del_item_id']); }
				return;
			break;
		}
	}
	
	public function runPostActions()
	{
		global $db;
		global $commander;
		global $adc;
		
		switch ($_POST['action'])
		{
			case 'list_type':
				if((count($_POST['Id']) > 0) && ($adc->getAdTypes()))
				{
					$i = 0;
					foreach($_POST['Id'] as $id)
					{
						if(isset($adc->AdTypes[$id]))
						{
							if($_POST['Title'][$i] !== $adc->AdTypes[$id]->getTitle())
							{ 
								$adc->AdTypes[$id]->setTitle($commander->formatText($_POST['Title'][$i]));
								$adc->AdTypes[$id]->setDataChanged(true);
							}
							if($_POST['TitleEn'][$i] !== $adc->AdTypes[$id]->getTitleEn())
							{ 
								$adc->AdTypes[$id]->setTitleEn($commander->formatText($_POST['TitleEn'][$i]));
								$adc->AdTypes[$id]->setDataChanged(true);
							}
							if($_POST['FormId'][$i] != $adc->AdTypes[$id]->getFormId())
							{ 
								$adc->AdTypes[$id]->setFormId($_POST['FormId'][$i]);
								$adc->AdTypes[$id]->setDataChanged(true);
							}
							if($_POST['FormBuyId'][$i] != $adc->AdTypes[$id]->getFormBuyId())
							{ 
								$adc->AdTypes[$id]->setFormBuyId($_POST['FormBuyId'][$i]);
								$adc->AdTypes[$id]->setDataChanged(true);
							}
							if($_POST['AdTypeObject'][$i] != $adc->AdTypes[$id]->getAdTypeObject())
							{ 
								$adc->AdTypes[$id]->setAdTypeObject($_POST['AdTypeObject'][$i]);
								$adc->AdTypes[$id]->setDataChanged(true);
							}
							$adc->AdTypes[$id]->update();
						}
						$i++;
					} //End foreach
					if($_POST['selected'])
					{
						foreach($_POST['selected'] as $id)
						{
							if(isset($adc->AdTypes[$id]))
							{
								if($_POST['activate'] == '-1')
								{
									$adc->AdTypes[$id]->setActive(1);
									$commander->setTableActive($id, 'am_types', 1); 
								}
								if($_POST['deactivate'] == '-1')
								{
									$adc->AdTypes[$id]->setActive(0);
									$commander->setTableActive($id, 'am_types', 0); 
								}
							}
						}
					}
				}
			break;
			//List-Editor group actions end
			
			//Editor AdType actions
			case 'editor_type':
				if($adc->getCurrentAdTypeId() > 0)
				{
					if(is_object($adc->getCurrentAdType()))
					{
						if(stripslashes($_POST['Title']) !== $adc->CurrentAdType->getTitle())
						{ 
							$adc->CurrentAdType->setTitle($commander->formatText($_POST['Title'])); 
							$adc->CurrentAdType->setDataChanged(true);
						}
						if(stripslashes($_POST['TitleEn']) !== $adc->CurrentAdType->getTitleEn())
						{ 
							$adc->CurrentAdType->setTitleEn($commander->formatText($_POST['TitleEn'])); 
							$adc->CurrentAdType->setDataChanged(true);
						}
						if(intval($_POST['FormId']) != $adc->CurrentAdType->getFormId())
						{ 
							$adc->CurrentAdType->setFormId(intval($_POST['FormId']));
							$adc->CurrentAdType->setDataChanged(true);
						}
						if(intval($_POST['FormBuyId']) != $adc->CurrentAdType->getFormBuyId())
						{ 
							$adc->CurrentAdType->setFormBuyId(intval($_POST['FormBuyId']));
							$adc->CurrentAdType->setDataChanged(true);
						}
						if(intval($_POST['AdTypeObject']) != $adc->CurrentAdType->getAdTypeObject())
						{ 
							$adc->CurrentAdType->setAdTypeObject(intval($_POST['AdTypeObject']));
							$adc->CurrentAdType->setDataChanged(true);
						}
						if(stripslashes($_POST['Description']) !== $adc->CurrentAdType->getDescription())
						{ 
							$adc->CurrentAdType->setDescription($commander->formatHTML($_POST['Description']));
							$adc->CurrentAdType->setDataChanged(true);
						}
						if($_FILES['Image'])
						{
							$file = $_FILES['Image'];
							if($file["name"] != '')
							{
								if($file['size'] < 1000000)
								{ $adc->CurrentAdType->uploadImage($_FILES['Image']); }
								else
								{ $commander->ErrorField['Image'] = 'image file size exceded 1mb'; }
							}
							$adc->CurrentAdType->setDataChanged(true);
						}
						if(intval($_POST['ImageDelete']) > 0)
						{ 
							$adc->CurrentAdType->deleteImage(); 
							$adc->CurrentAdType->setDataChanged(true);
						}
						$adc->CurrentAdType->update();
					}
				}
			break;
		}
	}
	
	function addAdType($title)
	{
		global $commander;
		$title = $commander->formatText($title);
		if($title != '')
		{
			$sql  = 'INSERT INTO '.DB_PERFIX.'am_types (`title`)'; 
			$sql .= 'VALUES ("'.$title.'");';
			$result = mysql_query($sql);
		}
	}
	
	function deleteAdType($id)
	{	
		if($id > 0)
		{	
			$sql = 'UPDATE '.DB_PERFIX.'am_categories SET typeId=0 WHERE typeId='.intval($id).';';
			$result = mysql_query($sql);
			$sql = 'DELETE FROM '.DB_PERFIX.'am_types WHERE id='.intval($id).' LIMIT 1;';
			$result = mysql_query($sql);
			return;
		}
	}
}
?>